package com.bjfu.educenter.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjfu.common_utils.JwtUtils;
import com.bjfu.common_utils.MD5;
import com.bjfu.common_utils.R;
import com.bjfu.educenter.entity.UcenterMember;
import com.bjfu.educenter.entity.vo.RegisterVo;
import com.bjfu.educenter.mapper.UcenterMemberMapper;
import com.bjfu.educenter.service.UcenterMemberService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjfu.servicebase.exceptionhandler.GuliException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author Guli
 * @since 2020-07-13
 */
@Service
public class UcenterMemberServiceImpl extends ServiceImpl<UcenterMemberMapper, UcenterMember> implements UcenterMemberService {

    @Autowired
    private RedisTemplate<String,String> redisTemplate;

    // 登录的方法
    @Override
    public String login(UcenterMember member) {
        // 获取登录手机号和密码
        String mobile = member.getMobile();
        String password = member.getPassword();
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(password)) {
            throw new  GuliException(20001,"手机号或密码为空");
        }

        // 判断手机号是否正确
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        UcenterMember mobileNumber = baseMapper.selectOne(wrapper);
        if(mobileNumber == null) throw new GuliException(20001,"手机号不存在");

        // 判断密码是否匹配
        // 数据库中的密码是加密的，因此要先把输入的密码加密
        // 加密方式MD5
        if(!MD5.encrypt(password).equals(mobileNumber.getPassword())){
            throw new GuliException(20001,"密码错误");
        }

        // 判断用户是否被禁用
        if(mobileNumber.getIsDisabled()){
            throw new GuliException(20001,"用户已被禁用");
        }

        // 登录成功
        // 按照规则生成token字符串，使用jwt工具类
        String jwtToken = JwtUtils.getJwtToken(mobileNumber.getId(), mobileNumber.getNickname());

        return jwtToken;
    }

    @Override
    public void register(RegisterVo registerVo) {
        // 获取注册的数据
        String code = registerVo.getCode();
        String mobile = registerVo.getMobile();
        String nickname = registerVo.getNickname();
        String password = registerVo.getPassword();

        // 手机号、用户名、密码判空
        if (StringUtils.isEmpty(mobile) || StringUtils.isEmpty(nickname) || StringUtils.isEmpty(password) ||StringUtils.isEmpty(code)) {
            throw new GuliException(20001,"注册信息不完整");
        }

        //取出redis中验证码
        String redisCode = redisTemplate.opsForValue().get(mobile);
        System.out.println(redisCode);

        if(!redisCode.equals(code)){
            throw new GuliException(20001,"验证码错误");
        }

        // 判断手机号是否存在，表里存在相同手机号不能添加
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("mobile",mobile);
        Integer count = baseMapper.selectCount(wrapper);
        if(count > 0){
            throw new GuliException(20001,"手机号已经存在");
        }

        UcenterMember member = new UcenterMember();
        member.setMobile(mobile);
        member.setPassword(MD5.encrypt(password));
        member.setNickname(nickname);
        member.setAvatar("https://edu478.oss-cn-hangzhou.aliyuncs.com/2020/07/03/2cb5996ab83b438fbfb257caca23ad85file.png");
        member.setIsDisabled(false);
        baseMapper.insert(member);
    }

    @Override
    public UcenterMember getMemberByOpenId(String openid) {
        QueryWrapper<UcenterMember> wrapper = new QueryWrapper<>();
        wrapper.eq("openid",openid);
        UcenterMember member = baseMapper.selectOne(wrapper);
        return member;
    }

    @Override
    public Integer countRegisterDay(String day) {
        Integer integer = baseMapper.countRegister(day);
        return integer;
    }
}
